﻿#region License

// Droid Manager - An Android Phone Tools Suite
// Copyright (C) 2011
// 
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

#endregion

using DroidManager.Data.Utils;
using System;
using System.IO;
using System.Windows.Forms;

namespace DroidManager.Data
{
    internal static class GlobalVars
    {
        internal static string AppDataPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
        internal static string DmDataPath = AppDataPath + @"\DroidManager";
        internal static string DmMainSettingsFilePath = DmDataPath + @"\MainSettings.ini";
        internal static string DmPluginSettingsFilePath = DmDataPath + @"\PluginsSettings.ini";
        internal static IniFile DmMainSettingsFile = new IniFile(DmMainSettingsFilePath);
        internal static IniFile DmPluginSettingsFile = new IniFile(DmPluginSettingsFilePath);

        internal static string CommonAppDataPath =
            Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);

        internal static string PluginPath = CommonAppDataPath + @"\DroidManager\Plugins";
        internal static string PluginSettingsPath = DmDataPath + @"\PluginsSettings";

        internal static string DmDeviceDataPath = DmDataPath + @"\Devices";

        internal static string DmLogPath = DmDataPath + @"\Log";

        internal static string AdbFilePath
        {
            get
            {
                if (File.Exists(DmMainSettingsFilePath))
                {
                    string adb = DmMainSettingsFile.IniReadValue("Settings", "AdbPath");
                    if (adb != "")
                    {
                        return adb;
                    }
                    else
                    {
                        return Application.StartupPath + @"\Resources\Binaries\Adb\adb.exe";
                    }
                }
                else
                {
                    return Application.StartupPath + @"\Resources\Binaries\Adb\adb.exe";
                }
            }
            set { DmMainSettingsFile.IniWriteValue("Settings", "AdbPath", value); }
        }

        internal static string AaptFilePath
        {
            get
            {
                if (File.Exists(DmMainSettingsFilePath))
                {
                    string aapt = DmMainSettingsFile.IniReadValue("Settings", "AaptPath");
                    if (aapt != "")
                    {
                        return aapt;
                    }
                    else
                    {
                        return Application.StartupPath + @"\Resources\Binaries\Aapt\aapt.exe";
                    }
                }
                else
                {
                    return Application.StartupPath + @"\Resources\Binaries\Aapt\aapt.exe";
                }
            }
            set { DmMainSettingsFile.IniWriteValue("Settings", "AaptPath", value); }
        }

        internal static void Initialize()
        {
            CheckSettings();
            LoadSettings();
        }

        private static void LoadSettings()
        {
            AdbFilePath = AdbFilePath;
        }

        private static void CheckSettings()
        {
            if (!Directory.Exists(DmDataPath))
            {
                Directory.CreateDirectory(DmDataPath);
            }

            if (!Directory.Exists(DmLogPath))
            {
                Directory.CreateDirectory(DmLogPath);
            }

            if (!File.Exists(DmMainSettingsFilePath))
            {
                DmMainSettingsFile.IniWriteValue("Settings", "AdbPath", AdbFilePath);
                DmMainSettingsFile.IniWriteValue("Settings", "AaptPath", AaptFilePath);
            }
            else
            {
                if (DmMainSettingsFile.IniReadValue("Settings", "AdbPath") == "")
                {
                    DmMainSettingsFile.IniWriteValue("Settings", "AdbPath", AdbFilePath);
                }

                if (DmMainSettingsFile.IniReadValue("Settings", "AaptPath") == "")
                {
                    DmMainSettingsFile.IniWriteValue("Settings", "AaptPath", AaptFilePath);
                }
            }


            if (!File.Exists(DmPluginSettingsFilePath))
            {
                //TODO Plugin Settings
            }

            if (!Directory.Exists(PluginPath))
            {
                Directory.CreateDirectory(PluginPath);
            }

            if (!Directory.Exists(PluginSettingsPath))
            {
                Directory.CreateDirectory(PluginSettingsPath);
            }

            if (!Directory.Exists(DmDeviceDataPath))
            {
                Directory.CreateDirectory(DmDeviceDataPath);
            }
        }
    }
}